﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Employee
{
    public partial class EmployeeList : Form
    {
        BindingList<EmployeeDTO> listEmpBinding;
        int rowIndex; //Biến lưu giá trị của dòng đang sửa hơặc vừa thêm mới
        EmployeeBUS employeeBUS;
        RuleObjectBUS RuleBUS;
        public EmployeeList()
        {
            InitializeComponent();
            dtgEmployeeList.AutoGenerateColumns = false;
            employeeBUS = new EmployeeBUS();
            listEmpBinding = new BindingList<EmployeeDTO>();
            rowIndex = 0;
            RuleBUS = new RuleObjectBUS();
        }
       

        private void EmployeeList_Load(object sender, EventArgs e)
        {
           // DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            listEmpBinding = employeeBUS.GetNewBindingListEmployee();
            dtgEmployeeList.DataSource = listEmpBinding;
            this.VisibleChanged += new EventHandler(EmployeeList_VisibleChanged);
            CheckPermission();
        }

        private void CheckPermission()
        {
            btnDel.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor);
            btnUpdate.Enabled = btnDel.Enabled;
        }

        void EmployeeList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                Global.GenerateNumber(dtgEmployeeList, colNumber.Index);
                Global.GenerateEditColumn(dtgEmployeeList, ColEdit.Index);
                Global.GenerateDeleteColumn(dtgEmployeeList, ColDel.Index);
            }
        }
        
       

        #region Public Methods

        public void EmployeeChanged(EmployeeDTO empDTO)
        {
            EmployeeDTO employeeDTO = employeeBUS.GetEmployeeByID(empDTO.EmployeeID);

            dtgEmployeeList[colEmployeeName.Index, rowIndex].Value = employeeDTO.EmployeeName;
        }

        public void EmployeeChangeAfterInsert(EmployeeDTO empDTO)
        {
            EmployeeDTO employeeDTO = employeeBUS.GetEmployeeByID(empDTO.EmployeeID);          

            listEmpBinding.Add(employeeDTO);

            rowIndex = dtgEmployeeList.Rows.GetLastRow(DataGridViewElementStates.None);
            dtgEmployeeList[ColEdit.Index, rowIndex].Value = Properties.Resources.edit_16;
            dtgEmployeeList[ColDel.Index, rowIndex].Value = Properties.Resources.deletered;
           
            if (rowIndex == 0)
            {
                dtgEmployeeList[colNumber.Index, rowIndex].Value = rowIndex + 1;
            }
            else
            {
                dtgEmployeeList[colNumber.Index, rowIndex].Value = dtgEmployeeList[colNumber.Index, rowIndex - 1].Value.ToString();
            }
        }

        #endregion Public Methods

        #region Button,Delete, Update

        private void btnDel_Click_1(object sender, EventArgs e)
        {
            DeleteEmployee();
        }
     
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dtgEmployeeList.Rows.Count > 0)
            {
                UpdateEmployee(dtgEmployeeList.CurrentRow.Index);
            }
        }

        private void dtgEmployeeList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == ColEdit.Index)
                {
                    UpdateEmployee(e.RowIndex);
                }
                else if (e.ColumnIndex == ColDel.Index)
                {
                    DeleteEmployee();
                }
            }
        }

        private void DeleteEmployee()
        {
            if (dtgEmployeeList.Rows.Count > 0)
            {
                if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionEmployee))
                {
                    Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                    return;
                }

                if (Global.ShowMessageBoxDelete("Bạn chắc chắn muốn xóa nhân viên này ?") == DialogResult.Yes)
                {
                    int empID = int.Parse(dtgEmployeeList[colEmployeeID.Index, dtgEmployeeList.CurrentRow.Index].Value.ToString());
                    if (employeeBUS.DeleteEmployee(empID))
                    {
                        Global.SetMessage(lblMessage, "Xóa thành công", true);
                        listEmpBinding.Remove(listEmpBinding.First(c => c.EmployeeID == empID));
                    }
                    else
                    {
                        Global.SetMessage(lblMessage, "Xóa không thành công", false);
                    }
                }
            }
        }

        private void UpdateEmployee(int index)
        {
            if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionEmployee))
            {
                Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                return;
            }

            rowIndex = index;
            int id = int.Parse(dtgEmployeeList[colEmployeeID.Index, rowIndex].Value.ToString());
            //EmployeeDTO employeeDTO = new EmployeeDTO
            //{
            //    EmployeeID = int.Parse(dtgEmployeeList[colEmployeeID.Index, rowIndex].Value.ToString()),
            //    EmployeeCode = dtgEmployeeList[colEmployeeCode.Index, rowIndex].Value.ToString(),
            //    EmployeeName = dtgEmployeeList[colEmployeeName.Index, rowIndex].Value.ToString(),
            //    EmployeeBirthDay = (DateTime)dtgEmployeeList[colBirthday.Index, rowIndex].Value,
            //    EmployeeEmail = dtgEmployeeList[colEmail.Index, rowIndex].Value.ToString(),
            //    EmployeePhone = dtgEmployeeList[colPhone.Index, rowIndex].Value.ToString(),
            //    EmployeeCMND = dtgEmployeeList[colCMND.Index, rowIndex].Value.ToString(),
            //    EmployeeSex = (bool)dtgEmployeeList[ColSex.Index, rowIndex].Value,
            //    EmployeeSexName = dtgEmployeeList[ColSexName.Index, rowIndex].Value.ToString(), 
            //    EmployeeAddress = dtgEmployeeList[colAddress.Index, rowIndex].Value.ToString(),
            //    EmployeeBeginDay = (DateTime)dtgEmployeeList[ColBeginDay.Index, rowIndex].Value,
            //    EmployeeKindCode = (int)dtgEmployeeList[ColKindCode.Index, rowIndex].Value,
            //    EmployeeStatus = (bool)dtgEmployeeList[colStatus.Index, rowIndex].Value,
            //    EmployeeNote = dtgEmployeeList[colNote.Index, rowIndex].Value.ToString()
            //};


            Employee.EmployeeDetail frm = new EmployeeDetail { EmployeeID = id, Action = ActionName.Update };
            //frm.OnCategoryChanged += new Book.FrmCategoryDetail.CategoryHasChanged(CategoryChanged);
           
            frm.ShowDialog();
        }

        #endregion Button, Delete, Update
    }
}
